package com.xy.job.admin.controller

import com.xy.job.admin.api.request.*
import com.xy.job.admin.api.response.PageDTO
import com.xy.job.admin.enums.ScheduleType
import com.xy.job.admin.service.JobInfoService
import com.xy.job.core.api.response.wrapperResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.constraints.NotNull
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

@Tag(name = "JobInfoApi")
@Validated
@RestController
@RequestMapping("/api/job")
class JobInfoController(
    var jobInfoService: JobInfoService
) {

    @Operation(summary = "查询任务列表")
    @PostMapping("/list")
    fun listJobInfo(@Validated @RequestBody param: JobInfoQueryParam) = wrapperResponse {
        val page = jobInfoService.query(param)
        return@wrapperResponse PageDTO(page)
    }

    @Operation(summary = "查询任务详情")
    @GetMapping("/detail")
    fun getJobInfo(@Validated jobId: Long) = wrapperResponse {
        return@wrapperResponse jobInfoService.get(jobId)
    }

    @Operation(summary = "新增任务")
    @PostMapping("/add")
    fun addJobInfo(@Validated @RequestBody param: JobInfoAddParam) = wrapperResponse {
        return@wrapperResponse jobInfoService.add(param)
    }

    @Operation(summary = "编辑任务")
    @PostMapping("/edit")
    fun editJobInfo(@Validated @RequestBody param: JobInfoEditParam) = wrapperResponse {
        return@wrapperResponse jobInfoService.edit(param)
    }

    @Operation(summary = "修改任务启用状态")
    @PostMapping("/switch")
    fun switchEnable(@Validated @RequestBody param: JobInfoSwitchParam) = wrapperResponse {
        return@wrapperResponse jobInfoService.switch(param)
    }

    @Operation(summary = "删除任务")
    @PostMapping("/remove")
    fun removeJobInfo(@NotNull jobId: Long) = wrapperResponse {
        return@wrapperResponse jobInfoService.remove(jobId)
    }

    @Operation(summary = "触发一次任务")
    @PostMapping("/triggerOnce")
    fun triggerOnce(@Validated @RequestBody param: JobTriggerManuallyParam) = wrapperResponse {
        return@wrapperResponse jobInfoService.triggerOnce(param)
    }

    @Operation(summary = "获取任务下次触发时间")
    @GetMapping("/nextTriggerTime")
    fun nextTriggerTime(scheduleType: ScheduleType, scheduleConfig: String, times: Int) = wrapperResponse {
        return@wrapperResponse jobInfoService.generateNextTriggerTime(
            scheduleType = scheduleType,
            scheduleConfig = scheduleConfig,
            times = times,
        )
    }
}